address_book.admin = liwe.module ( 'address_book.admin', null, "Rubrica" );

address_book.admin._dest_div = "system-content";
address_book.admin._kind_cache = []; 
address_book.admin._importable = {};
address_book.admin._visible_cnt = '';
address_book.admin._tabs = null;
address_book.admin._cur_id_parent = 0;
address_book.admin._cur_id_parent_name = '';

//{{{address_book.admin._init = function ( dest_div )
address_book.admin._init = function ( dest_div )
{
	if ( dest_div ) address_book.admin._dest_div =  dest_div;
	var _acts = [
		{ label: 'Profili', func: function () { address_book.admin._render_profile();} },
		{ label: 'Tipo Indirizzo', func: function () { address_book.admin._render_kind (); } },
		{ label: 'Categorie', func: function () { address_book.admin._render_category (); } }
	];
	address_book.admin.register ( _acts );
	address_book.admin._am_kind_init ( 'list_address_kind' );
	address_book.admin._am_profile_init ( 'list_address_profile' );
	address_book.admin._am_address_init ( 'list_address_address' );
	address_book.admin._am_category_init( 'ab_list_category');
	address_book.admin._am_sub_category_init();
	address_book.admin._set_kind_cache();
};
//}}}
//{{{ ADDRESS PROFILE SECTION
//{{{address_book.admin._show_cnt = function ( cnt_to_show )
address_book.admin._show_cnt = function ( cnt_to_show )
{
	var _el;
	if ( address_book.admin._visible_cnt != '' )
	{ 
		liwe.fx.fade_out ( address_book.admin._visible_cnt );
		_el = $el ( address_book.admin._visible_cnt );
		_el.style.display = "none";
	};
	_el = $el ( cnt_to_show );
	_el.style.display = "block";
	liwe.fx.fade_in ( cnt_to_show );
	address_book.admin._visible_cnt = cnt_to_show;
};
//}}}
//{{{address_book.admin._render_profile = function ()
address_book.admin._render_profile = function ()
{
	address_book._set_category_opts( function ()
	{
		$( address_book.admin._dest_div, address_book.templates [ 'ADMIN_PROFILE_LAYOUT' ] );
		address_book.admin._profile_refresh ();
	} );
};
//}}}
//{{{address_book.admin._profile_refresh =  function ( cback )
address_book.admin._profile_refresh =  function ( cback )
{
        address_book.admin.am_profile.show_list ();
	if ( cback ) cback();
};
//}}}
//{{{address_book.admin._am_profile_init = function ( dest_div )
address_book.admin._am_profile_init = function ( dest_div )
{
        if ( address_book.admin.am_profile ) return;
        address_book.admin.am_profile = new AdminManager
                                ( {
                                        'dest_res': dest_div,
                                        'name' : 'am_profile',
                                        'list_action': "address_book.admin.list_profile",
                                        'mod_menu_actions': [
                                                                 { func: "address_book.admin._profile_import()", 'class' : 'add' , label: 'Aggiungi Profilo' }
										],
                                        'filter_form' : address_book.admin._profile_filter_form,
                                        'filter_form_name' : "am_profile_filter",
                                        'del_action' : 'address_book.admin.del_profile',
					'lines_per_page' : 30
                                } );

        address_book.admin.am_profile.set_row_fields (
                                [
                                        [ "ID", "id_profile" ],
                                        [ "Tipo", "_kind" ],
                                        [ "Attivit&agrave;", "_category" ],
                                        [ "Azienda", "_company" ],
                                        [ "Persona Fisica", "_individual" ],
                                        [ "Nome/Ragione Sociale", "name" ],
                                        [ "Cognome/Referente", "lastname" ],
                                        [ "Livello", "level" ],
                                        [ "Sconto", "discount" ],
                                        [ "Codice Fiscale", "tax_code" ],
                                        [ "Partita IVA", "vat_code" ],
                                        [ "Data Inserimento", "_created" ]
                                ]
                                );

        address_book.admin.am_profile.cbacks [ 'row_manip' ] = address_book.admin._rwman_profile;
        address_book.admin.am_profile.cbacks [ 'del_item' ]  = function ( id ) { address_book.admin.del_profile ( id ); };
        address_book.admin.am_profile.cbacks [ 'edit_item' ]  = function ( id ) { address_book.admin.edit_profile ( id ); };
        address_book.admin.am_profile.cbacks [ 'noresult' ] = function () { address_book.admin._profile_import (); };
        address_book.admin.am_profile.cbacks [ 'show_results' ] = function () { address_book.admin._profile_show_list (); };
};
//}}}
//{{{address_book.admin._am_profile_filter_form = function ( dest_div )
address_book.admin._profile_filter_form = function ( dest_div )
{
	var f = new liwe.form.instance ( 'am_profile_filter' );
	f.number ( { 'label' : 'ID Utente' , 'name' : 'uid', size : 10  });
	f.text ( { 'label' : 'Nome/Ragione sociale', 'name' : 'name', size : 30, 'maxlength' : 30 , nonl : true } );
	f.text ( { 'label' : 'Cognome', 'name' : 'lastname', size : 30, 'maxlength' : 30, nonl : true } );
	f.email ( { 'label' : 'e-mail', 'name' : 'email' , validator : true, 'mandatory' : true } );
	f.text ( { 'label' : 'Indirizzo', 'name' : 'addr1', size : 30, 'maxlength' : 30 } );
	f.text ( { 'label' : 'Provincia', 'name' : 'prov', size : 30, 'maxlength' : 30, nonl : true } );
	f.text ( { 'label' : 'Citt&agrave;', 'name' : 'city', size : 30, 'maxlength' : 30, nonl : true } );
	f.text ( { 'label' : 'C.A.P.', 'name' : 'zip_code', size : 5, 'maxlength' : 5 } );
	f.text ( { 'label' : 'Nazione', 'name' : 'country', size : 30, 'maxlength' : 30 } );
	f.select ( { 'label' : 'Area Attivit&agrave;', 'name' : 'id_categ', 'options' : address_book._category_opts, force_select : true  } );
	f.button ( { value: "Applica Filtro", "class" : "green", colspan : 3,onclick: "address_book.admin._profile_filter()", nonl: true } );
	f.button ( { value: "Svuota", "class": "yellow", onclick: "address_book.admin._profile_refresh()" } );
	f.set ( dest_div );
};
//}}}
//{{{address_book.admin._am_profile_filter = function ()
address_book.admin._profile_filter = function ()
{
	address_book.admin.am_profile.search_start ();
};
//}}}
//{{{address_book.admin._rwman_profile = function ( ds, row )
address_book.admin._rwman_profile = function ( ds, row )
{
	row [ "_id" ] = row [ 'id_profile' ];
	row [ "_created" ] = row [ 'created' ].split( " ") [ 0 ];
	var id_categ = row [ 'id_categ' ]
	var category = address_book._category_cache.get (  id_categ , {} );
	row [ "_category" ] = category.get ( 'text', "Indefinita" ) ;
	var _bullet =  [ 'ico state red', 'ico state green' ];
	var _company = row [ 'company' ];
	var _individual = row [ 'individual' ];
	row [ '_company' ] = String.formatDict ( system.templates [ 'ADMIN_ROW_ICO'], { '_class' : _bullet [ _company ] } );
	row [ '_individual' ] = String.formatDict ( system.templates [ 'ADMIN_ROW_ICO'], { '_class' : _bullet [ _individual ] } );

	row [ '_kind' ] = String.formatDict ( system.templates [ 'ADMIN_ROW_ICO'], { '_class' : 'ico user' } );
	if  ( row [ 'uid' ] == 0 ) 
		row [ '_kind' ] = String.formatDict ( system.templates [ 'ADMIN_ROW_ICO'], { '_class' : 'ico user profile' } );


	
		
};
//}}}
//{{{address_book.admin.edit_profile = function ( id )
address_book.admin.edit_profile = function ( id )
{
        var _post = { action : 'address_book.ajax.get_profile', id_profile : id };
        address_book.ajax ( _post , function ( resp )
        {
			var data = resp.profile
			address_book.get_location_opts ( function () { address_book.admin._profile_form ( data ) }, null, data );
        });
};
//}}}
//{{{address_book.admin.add_profile = function ()
address_book.admin.add_profile = function ()
{
	var f = liwe.form.get ( 'am_profile_import_form' );
	var _uid = address_book.admin._importable.get( [ f.get_value ( 'uid' ) ], 0 ) ; 
	if ( _uid ==  0 ) return;
	address_book.admin._profile_form ( _uid );
};
//}}}
address_book.admin.add_new_profile = function()
{
	address_book.admin._profile_form ( null )
};

//{{{address_book.admin._profile_show_list =  function ()
address_book.admin._profile_show_list =  function ()
{
	 address_book.admin._show_cnt ( 'ab_profile_list' );	
};
//}}}
//{{{address_book.admin._profile_import = function ()
address_book.admin._profile_import = function ()
{
	var _post = { 'action' : 'address_book.admin.list_importable' };
	address_book.admin.ajax ( _post, function ( _resp )
	{
		var _user_opts = [];
		_resp.user.iterate ( function ( user )
		{
			var _uid = user  [ 'uid' ];
			_user_opts.push ( { 'label' : user [ 'login' ], 'value' : _uid } )
			address_book.admin._importable [ _uid ] = user;
		});
		
		address_book.get_location_opts ( function () 
		{ 
			address_book.admin.profile_import_form ( _user_opts );
			address_book.admin._show_cnt ( 'ab_profile_import' );
		}, null, _user_opts );
	});
}
//}}}
//{{{address_book.admin.profile_import_form = function ( user_opts )
address_book.admin.profile_import_form = function ( user_opts )
{
	var f = new liwe.form.instance ( 'am_profile_import_form' );
	f.select ( { 'label' : 'Seleziona Utente da importare', 'name' : 'uid', 'options' : user_opts, force_select : true, 'onChange' : 'address_book.admin._import_setform( this )' } );
	f.label ( { 'value' : '<div id="ab_import_login"></div>' } ); 
	f.label ( { 'value' : '<div id="ab_import_mail"></div>' } ); 
        f.button ( { value: 'Importa', 'class' : "blue", onclick : "address_book.admin.add_profile()"  } );
        f.button ( { value: 'Solo Profilo', 'class' : "green", onclick : "address_book.admin.add_new_profile();" } );
        f.button ( { value: 'Nuovo Utente', 'class' : "red", onclick : "address_book.admin.add_new_user_profile();" } );
        f.button ( { value: 'Annulla', 'class' : "grey", onclick : "address_book.admin._profile_refresh()" } );
	f.set ( 'form_address_profile_import' );
}
//}}}
//{{{address_book.admin._import_setform =  function ( el ) 
address_book.admin._import_setform =  function ( el ) 
{
	var _user =  address_book.admin._importable [ el.value ];
	$( 'ab_import_login', _user [ 'login' ] );
	$( 'ab_import_mail', _user [ 'email' ] );
}
//}}}
//{{{address_book.admin._profile_form = function ()
address_book.admin._profile_form = function ( data, no_reload )
{
	if ( data  == null )  data = {};
	var _level_opts = [ { label: "1", value: "1" },
			   { label: "2", value: "2" },
			   { label: "3", value: "3" },
			   { label: "4", value: "4" },
			   { label: "5", value: "5" },
			   { label: "6", value: "6" },
			   { label: "7", value: "7" },
			   { label: "8", value: "8" },
			   { label: "9", value: "9" },
			   { label: "10", value: "10" } ]

	var f =  new liwe.form.instance ( 'am_profile_form' );
	f.events [ 'complete' ] = address_book.admin._profile_save;
	f.hidden ( "action", 'address_book.ajax.set_profile' );
	f.hidden ( 'uid', data.get ( 'uid', 0 )  );
	f.hidden ( 'id_profile', data.get ( 'id_profile', 0 )  );
	f.text ( { 'label' : 'Nome/Ragione Sociale', 'name' : 'name', size : 30, 'maxlength' : 30, mandatory : true, value : data.get ( 'name' , '' ) } );
	f.text ( { 'label' : 'Cognome', 'name' : 'lastname', size : 30, 'maxlength' : 30,  value : data.get ( 'lastname' , '' )} );
	f.select ( { 'label' : 'Livello', 'name' : 'level', 'options' : _level_opts, force_select : true , value : data.get ( 'level' , '' )} );
	f.checkbox ( { label: 'Azienda', name: 'company', checked : data.get ( 'company' , 0 ) ,  value : 1 } );
	f.checkbox ( { label: 'Persona fisica', name: 'individual', checked : data.get ( 'individual', 0 ),  value : 1 } );
	f.sep ();
	f.cod_fisc ( { 'label' : 'Cod. Fiscale', 'name' : 'tax_code', size : 30, 'maxlength' : 20, 'width' : '100%', value : data.get ( 'tax_code' , '' ) } );
	f.p_iva ( { 'label' : 'P.IVA', 'name' : 'vat_code', size : 30, 'maxlength' : 20,  value : data.get ( 'vat_code' , '' ) } );
	f.number ( { 'label' : 'Sconto (%)' , 'name' : 'discount', size : 10, 'maxlength' : 4, value : data.get ( 'dicount' , '' ) });
	f.email ( { 'label' : 'e-mail', 'name' : 'email', size : 10, 'maxlength' : 50,  value : data.get ( 'email' , '' ) } );
	f.text ( { 'label' : 'Telefono', 'name' : 'phone', size : 10, 'maxlength' : 50,  value : data.get ( 'phone' , '' ) } );
	f.text ( { 'label' : 'Cellulare', 'name' : 'cell', size : 10, 'maxlength' : 50, value : data.get ( 'cell' , '' ) } );
	f.text ( { 'label' : 'FAX', 'name' : 'fax', size : 10, 'maxlength' : 50, value : data.get ( 'fax' , '' ) } );
	f.sep ();
	f.select ( { 'label' : 'Stato', 
				 'name' : 'country', 
				 'options' : address_book._loc_opts [ 'stati' ], 
				 force_select : true , 
				 onchange: "address_book.change_stato( this, address_book.admin._profile_form, 'am_profile_form')", 
				 value : data.get ( 'country' , 'Italia' )} );
	if ( data.get ( 'country', 'Italia' ) == 'Italia' )
	{
		f.select ( { 'label' : 'Provincia', 
					 'name' : 'prov', 'options' : address_book._loc_opts  [ 'province' ], 
					 onchange: "address_book.change_prov( this, address_book.admin._profile_form, 'am_profile_form')", 
					 force_select : true ,  
					 value : data.get ( 'prov' , '' )} );

		if ( data.get ( 'prov' ) )
		{
			f.select ( { name: "city", label: "Citt&agrave;", 
						 mandatory: true, options: address_book._loc_opts [ data.prov ], 
						 force_select: true, 
						 onchange: "address_book.change_comu( this, address_book.admin._profile_form, 'am_profile_form')", 
						 value: data.get ( 'city', '' ) } );
		};
		if ( data.get ( 'city' ) )
		{
			f.select ( { name: "zip_code", label: "C.A.P.", mandatory: true, options: address_book._loc_opts [ data.prov + data.city ], value: data.get ( 'zip_code', '' ) } );
		};
		f.text ( { 'label' : 'Indirizzo', 'name' : 'addr1' , 'size' : 100 , 'maxlength' : 50, 'colspan' : 5, value : data.get ( 'addr1', '' ) } );
	} else {
		f.textarea ( { 'label' : 'Indirizzo', 'name' : 'addr1' , 'rows' : 5 , 'cols' : 50, value : data.get ( 'addr1', '' ) } );
	};
	f.select ( { 'label' : 'Area Attivit&agrave;', 'name' : 'id_categ', 'options' : address_book._category_opts, force_select : true , value : data.get ( 'id_categ' , 0 )} );
	f.sep ();
	f.jconed ( { 'label' : 'Note', 'name' : "note", width: 300, height: 200, colspan : 5, value : data.get ( 'note' , '' ) } );
	f.submit ( { value: 'Salva', 'class' : "button green",nonl : true, colspan : 2} );
	f.button ( { value: 'Annulla', 'class' : "grey", onclick : "address_book.admin._profile_refresh()", colspan : 2 } );
	f.set ( 'form_address_profile' );

	if ( no_reload ) return;
	//CREATE TABS
	address_book.admin._init_tabs ();
	$( 'ab_profile_address_cnt', address_book.templates [ 'ADMIN_ADDRESS_LAYOUT' ] );
	address_book.admin._address_refresh();
	//SET ADDRESS
	address_book.admin._tabs.send_event ( 'ab_profile_address_cnt', 'click' );
	address_book.admin._show_cnt ( 'ab_profile_form' );
};
//}}}
//{{{address_book.admin._init_tabs = function ()
address_book.admin._init_tabs = function ()
{
	var s = new String.buffer ();
	var res;
	if ( ! address_book.admin._tabs ) address_book.admin._tabs = new OS3Tabs.instance ( 'ab_tabs' );

	address_book.admin._tabs.clear ();

	s.add ( address_book.templates [ 'ADMIN_PROFILE_TABS_START' ] );

	s.add ( String.formatDict ( address_book.templates [ 'ADMIN_PROFILE_TAB' ], { "tab_name" : "ab_profile_address_cnt" } ) );
	address_book.admin._tabs.add ( 'Indirizzi', 'ab_profile_address_cnt' );

	s.add ( address_book.templates [ 'ADMIN_PROFILE_TABS_END' ] ); 

	res = s.toString ();
	$( 'ab_profile_tabs', res );
	address_book.admin._tabs.render ();
};
//}}}
//{{{address_book.admin._profile_save =  function ()
address_book.admin._profile_save =  function ()
{
	address_book.admin._profile_refresh ( function () 
	{
		liwe.utils.notifier.show ( 'Profilo Salvato correttamente', 'info', 2000)
	});	
};
//}}}
//{{{address_book.admin.del_profile = function ( ids )
address_book.admin.del_profile = function ( ids )
{
        var _post = { 'action' : 'address_book.admin.del_profile', 'uids' : ids };
        address_book.admin.ajax ( _post , function ()
        {
		address_book.admin._profile_refresh ( function () 
		{
			liwe.utils.notifier.show ( 'Profilo Eliminato correttamente', 'info', 2000)
		});	
        });
}; 
//}}}
//}}}
//{{{ ADDRESS ADDRESS SECTION
//{{{address_book.admin._address_refresh =  function ( cback )
address_book.admin._address_refresh =  function ( cback )
{
        address_book.admin.am_address.show_list ();
	if ( cback ) cback();
};
//}}}
//{{{address_book.admin._am_address_init = function ( dest_div )
address_book.admin._am_address_init = function ( dest_div )
{
        if ( address_book.admin.am_address ) return;
        address_book.admin.am_address = new AdminManager
                                ( {
                                        'dest_res': dest_div,
                                        'name' : 'am_address',
                                        'list_action': "address_book.ajax.list_address",
                                        'mod_menu_actions': [
                                                                 { func: "address_book.admin.add_address()", 'class' : 'add' , label: 'Aggiungi Indirizzo' }
					],

					'filter_form' : address_book.admin._address_filter_form,
					'filter_form_name' : "ab_address_filter"
                                } );

        address_book.admin.am_address.set_row_fields (
                                [
                                        [ "id", "id" ],
                                        [ "Tipo", "kind_name" ],
                                        [ "Indirizzo 1", "addr1" ],
                                        [ "Indirizzo 2", "addr2" ],
                                        [ "C.A.P.", "zip_code" ],
                                        [ "Citta'", "city" ],
                                        [ "Provincia", "prov" ],
                                        [ "Stato - Regione", "country" ],
                                        [ "Telefono", "phone" ],
                                        [ "Data Inserimento", "_created" ]
                                ]
                                );

        address_book.admin.am_address.cbacks [ 'row_manip' ] = address_book.admin._rwman_address;
        address_book.admin.am_address.cbacks [ 'noresult' ] = function () { address_book.admin.add_address (); };
        address_book.admin.am_address.cbacks [ 'del_item' ]  = function ( id ) { address_book.admin.del_address ( id ); };
        address_book.admin.am_address.cbacks [ 'edit_item' ]  = function ( id ) { address_book.admin.edit_address ( id ); };
	address_book.admin.am_address.templates [ 'no_result' ] =  '';
};
//}}}
//{{{address_book.admin._rwman_address = function ( ds, row )address_book.admin._address_showlist
address_book.admin._rwman_address = function ( ds, row )
{
        row [ "_id" ] = row [ 'id' ];
	row [ "_created" ] = row [ 'created' ].split( " ") [ 0 ] 
};
//}}}
//{{{address_book.admin._address_form = function ( data, dest_div )
address_book.admin._address_form = function ( data )
{
	var dest_div = 'form_address_address'	;
	var f = new liwe.form.instance ( 'am_address_form' );
	f.events [ 'complete' ] = address_book.admin.save_address;
	f.hidden ( "action", 'address_book.ajax.set_address' );
	f.hidden ( 'id_address' , data.get ( 'id', 0 ) );
	f.hidden ( 'id_profile', data.get ( 'id_profile' , 0 ) );
	f.select ( { 'label' : 'Stato', 
				 'name' : 'country', 
				 'options' : address_book._loc_opts [ 'stati' ], 
				 force_select : true , 
				 onchange: "address_book.change_stato( this, address_book.admin._address_form, 'am_address_form')", 
				 value : data.get ( 'country' , 'Italia' )} );
	if ( data.get ( 'country', 'Italia' ) == 'Italia' )
	{
		f.select ( { 'label' : 'Provincia', 
					 'name' : 'prov', 'options' : address_book._loc_opts  [ 'province' ], 
					 onchange: "address_book.change_prov( this, address_book.admin._address_form, 'am_address_form')", 
					 force_select : true ,  
					 value : data.get ( 'prov' , '' )} );

		if ( data.get ( 'prov' ) )
		{
			f.select ( { name: "city", label: "Citt&agrave;", 
						 mandatory: true, options: address_book._loc_opts [ data.prov ], 
						 force_select: true, 
						 onchange: "address_book.change_comu( this, address_book.admin._address_form, 'am_address_form')", 
						 value: data.get ( 'city', '' ) } );
		};
		//f.text ( { 'label' : "Citt&agrave;", 'name' : 'city' , size : 30, 'maxlength' : 30, value : data.get ( 'city', '' ) } );
		if ( data.get ( 'city' ) )
		{
			f.select ( { name: "zip_code", label: "C.A.P.", mandatory: true, options: address_book._loc_opts [ data.prov + data.city ], value: data.get ( 'zip_code', '' ) } );
		};
		f.text ( { 'label' : 'Indirizzo', 'name' : 'addr1' , 'size' : 100 , 'maxlength' : 50, 'colspan' : 5, value : data.get ( 'addr1', '' ) } );
		f.text ( { 'label' : 'Indirizzo secondario', 'name' : 'addr2' , size : 50, 'maxlength' : 50, 'colspan' : 6, value : data.get ( 'addr2', '' ) } );
	} else {
		f.textarea ( { 'label' : 'Indirizzo', 'name' : 'addr1' , 'rows' : 5 , 'cols' : 50, value : data.get ( 'addr1', '' ) } );
		f.textarea ( { 'label' : 'Indirizzo secondario', 'name' : 'addr2' ,  'rows' : 5 , 'cols' : 50,  value : data.get ( 'addr2', '' ) } );
	};
	f.select ( { 'label' : 'Tipo', 'name' : 'id_addr_kind', 'options' : address_book.admin._get_kind_opts(), force_select : true,  value : data.get ( 'id_addr_kind', 0 ) } );
	f.text ( { 'label' : "Telefono", 'name' : 'phone' , size : 30, 'maxlength' : 30,  value : data.get ( 'phone', '' ) } );
	f.email ( { 'label' : "e-mail", 'name' : 'mail' , size : 30, 'maxlength' : 30, value : data.get ( 'mail', '' ) } );
	
	f.submit ( { value: 'Salva', 'class' : "button green",nonl : true, 'colspan' : 2 } );
	f.button ( { value: 'Annulla', 'class' : "grey", 'style' : 'text-align:right' , onclick : "liwe.dom.del_class ( $( 'form_address_address' ), 'show' )", colspan : 2 } );
	f.set ( dest_div );
	liwe.dom.add_class ( $( dest_div ), 'show' );
};
//}}}
//{{{address_book.admin.add_address =  function ()
address_book.admin.add_address =  function ()
{
	var x = liwe.form.get ( 'am_profile_form' );
 	var _data =  x.get_values();
	address_book.admin._address_form ( _data )
};
//}}}
//{{{address_book.admin.edit_address = function ( id )
address_book.admin.edit_address = function ( id )
{
        var _post = { action : 'address_book.ajax.get_address', id_address : id };
        address_book.ajax ( _post , function ( _resp )
        {      
		address_book.admin._address_form ( _resp.address )
        });
};
//}}}
//{{{address_book.admin.save_address =  function ()
address_book.admin.save_address =  function ()
{
	address_book.admin._address_refresh ( function () 
	{
		liwe.dom.del_class ( $( 'form_address_address' ), 'show' );	
		liwe.utils.notifier.show ( 'Indirizzo Salvato correttamente', 'info', 2000)
	});
};
//}}}
//{{{address_book.admin.del_address = function ( ids )
address_book.admin.del_address = function ( ids )
{
        var _post = { 'action' : 'address_book.admin.del_address', 'ids' : ids };
        address_book.admin.ajax ( _post , function ()
        {
		address_book.admin._address_refresh ( function () 
		{
			liwe.utils.notifier.show ( 'Indirizzo Eliminato correttamente', 'info', 2000)
		});
        });
}; 
//}}}
//{{{address_book.admin._address_filter_form = function ( dest_div )
address_book.admin._address_filter_form = function ( dest_div )
{
	var x = liwe.form.get ( 'am_profile_form' ); 
	var f = new liwe.form.instance ( "ab_address_filter" );
	f.hidden ( "id_profile", x.get_value ( 'id_profile' ) );
	f.set ( dest_div );
};
//}}}
//}}}
//{{{ ADDRESS KIND SECTION
//{{{address_book.admin._get_kind_opts = function()
address_book.admin._get_kind_opts = function()
{
	var ret = []
	address_book.admin._kind_cache.iterate ( function ( el )
	{
		ret.push ( { 'label' : el [ 'descr' ], 'value' : el [ 'id' ] } );
	});
	return ret;
};
//}}}
//{{{address_book.admin._set_kind_cache = function ()
address_book.admin._set_kind_cache = function ()
{
	var _post = { 'action' : 'address_book.ajax.list_kind' };
	address_book.admin.ajax ( _post, function ( _resp )
	{
		address_book.admin._kind_cache = [];
		_resp.kind.iterate ( function ( _kind )
		{
			address_book.admin._kind_cache.push ( _kind );
		}); 	
	});
};
//}}}
//{{{address_book.admin._render_kind = function ()
address_book.admin._render_kind = function ()
{
	$( address_book.admin._dest_div, address_book.templates [ 'ADMIN_KIND_LAYOUT' ] );
	address_book.admin._kind_refresh ();
};
//}}}
//{{{address_book.admin._am_kind_init = function ( dest_div )
address_book.admin._am_kind_init = function ( dest_div )
{
        if ( address_book.admin.am_kind ) return;
        address_book.admin.am_kind = new AdminManager
                                ( {
                                        'dest_res': dest_div,
                                        'name' : 'am_kind',
                                        'list_action': "address_book.ajax.list_kind",
                                        'mod_menu_actions': [
                                                                 { func: "address_book.admin.add_kind()", 'class' : 'add' , label: 'Aggiungi Tipo Indirizzo' }
					]
                                } );

        address_book.admin.am_kind.set_row_fields (
                                [
                                        [ "id", "id" ],
                                        [ "Descrzione", "descr" ],
                                        [ "Creato", "_created" ]
                                ]
                                );

        address_book.admin.am_kind.cbacks [ 'row_manip' ] = address_book.admin._rwman_kind;
        address_book.admin.am_kind.cbacks [ 'del_item' ]  = function ( id ) { address_book.admin.del_kind ( id ); };
        address_book.admin.am_kind.cbacks [ 'edit_item' ]  = function ( id ) { address_book.admin.edit_kind ( id ); };
        address_book.admin.am_kind.cbacks [ 'noresult' ] = function () { address_book.admin.add_kind (); };
};
//}}}
//{{{address_book.admin._rwman_kind = function ( ds, row )
address_book.admin._rwman_kind = function ( ds, row )
{
    row [ "_id" ] = row [ 'id' ];
	row [ "_created" ] = row [ 'created' ].split( " ") [ 0 ] 
};
//}}}
//{{{address_book.admin._kind_refresh =  function ( cback )
address_book.admin._kind_refresh =  function ( cback )
{
    address_book.admin.am_kind.show_list ();
	address_book.admin._kind_form ();
	if ( cback ) cback();
};
//}}}
//{{{address_book.admin._kind_form = function ()
address_book.admin._kind_form = function ()
{
        var f = new liwe.form.instance ( 'am_kind_form' );
	f.events [ 'complete' ] = address_book.admin._kind_save;
	f.hidden ( "action", 'address_book.admin.set_kind' );
	f.hidden ( 'id_kind', 0  );
	f.hidden ( 'owner', system.admin._user_info [ 'uid'] );
	f.label ( { 'value' : 'Nome' } );
        f.text ( { 'name' : 'descr', 'size' : 20, 'maxlength' : 50, mandatory : true } );
        f.submit ( { value: 'Salva', 'class' : "button green",nonl : true } );
        f.button ( { value: 'Annulla', 'class' : "grey", onclick : "address_book.admin._kind_refresh()" } );
        f.set ( 'ab_kind_form' );
	$ ( "ab_kind_title", "Nuova Tipologia" );
};
//}}}
//{{{address_book.admin.add_kind = function ()
address_book.admin.add_kind = function ()
{
	address_book.admin._kind_form ();
};
//}}}
//{{{address_book.admin.edit_kind = function ( id )
address_book.admin.edit_kind = function ( id )
{
        var _post = { action : 'address_book.admin.get_kind', id_kind : id };
        address_book.ajax ( _post , function ( _resp )
        {      
		var f = liwe.form.get ( 'am_kind_form' );
                f.set_value ( 'descr', _resp.kind [ 'descr' ]  );
                f.set_value ( 'id_kind', _resp.kind [ 'id' ]  );
		$ ( "ab_kind_title", "Modifica Tipologia" );
        });
};
//}}}
//{{{address_book.admin._kind_save =  function ()
address_book.admin._kind_save =  function ()
{
	address_book.admin._kind_refresh ( function () 
	{
		liwe.utils.notifier.show ( 'Tipologia Salvata correttamente', 'info', 2000)
		address_book.admin._set_kind_cache ();	
	});	
};
//}}}
//{{{address_book.admin.del_kind = function ( ids )
address_book.admin.del_kind = function ( ids )
{
        var _post = { 'action' : 'address_book.admin.del_kind', 'ids' : ids };
        address_book.admin.ajax ( _post , function ()
        {
		liwe.utils.notifier.show ( 'Tipologia Eliminata correttamente', 'info', 2000)
                address_book.admin.am_kind.show_list();
        });
};  
//}}} 
//}}}}
//{{{ADDRESS CATEGORY AND SUB CATEGORY SECTION
//{{{address_book.admin._render_category = function ()
address_book.admin._render_category = function ()
{
	$( address_book.admin._dest_div, address_book.templates [ 'ADMIN_CATEGORY_LAYOUT' ] );
	address_book.admin._category_refresh ();
	address_book.admin.add_category();
};
//}}}
//{{{address_book.admin._category_refresh =  function ( cback )
address_book.admin._category_refresh =  function ( cback )
{
    address_book.admin._am_category.show_list ();
	if ( cback ) cback();
};
//}}}
//{{{address_book.admin._am_category_init = function ( dest_div )
address_book.admin._am_category_init = function ( dest_div )
{
	if ( address_book.admin._am_category ) return;
	address_book.admin._am_category = new AdminManager
							( {
									'dest_res'    : dest_div,
									'name'		: 'am_category',
									'list_action' : 'address_book.admin.list_am_categories',
									'mod_menu_actions': 
									[
										{ func: "address_book.admin.add_category()", 'class' : 'add' , label: 'Nuova Categoria' }
									]
							} );


	address_book.admin._am_category.set_row_fields (
							[
									[ "Nome", "text" ]
							]
							);

	address_book.admin._am_category.set_row_actions ( 
						[ 
							[ address_book.templates [ 'ADMIN_SUB_CATEGORY_ACTION' ], "sub_category" ]
						] );

	address_book.admin._am_category.cbacks [ 'row_manip' ] = address_book.admin._rwman_category;
	address_book.admin._am_category.cbacks [ 'edit_item' ] = function ( id ) { address_book.admin.edit_category ( id ); };
	address_book.admin._am_category.cbacks [ 'del_item' ] = function ( id ) { address_book.admin.del_category ( id ); };
	address_book.admin._am_category.cbacks [ 'sub_category' ] = function ( id ) { address_book.admin.show_sub_category ( id ); };
	address_book.admin._am_category.cbacks [ 'pre_del' ] = address_book.admin._cat_pre_del;

	address_book.admin._am_category.templates [ 'module_single_action' ] = address_book.templates [ 'ADMIN_MODULE_SINGLE_ACTION' ];
	address_book.admin._am_category.templates [ 'actions_row_action' ] = address_book.templates [ 'ADMIN_CATEGORY_ROW_ACTION' ];
};
//}}}
//{{{address_book.admin._rwman_category = function ( ds, row )
address_book.admin._rwman_category = function ( ds, row )
{
	row [ '_id' ] = row [ 'id' ];
};
//}}}
//{{{address_book.admin._cat_pre_del = function ( am )
address_book.admin._cat_pre_del = function ( am )
{
	var ids = am.get_selected ();
	if ( ids == -1 ) return true; 
	ids = "|".join ( ids )
	address_book.admin.del_category ( ids )	
	return false;
};
//}}}
//{{{address_book.admin._category_form = function ()
address_book.admin._category_form = function ()
{
        var f = new liwe.form.instance ( 'category_add' );
        f.hidden ( 'id' , '0' );
        f.hidden ( 'id_node' , address_book.admin._cur_id_parent );
        f.hidden ( 'id_parent' , '0' );
        f.text ( { 'name' : 'text', 'size' : 20, 'maxlength' : 50, 'mandatory' : true, 'label' : 'Categoria' } );
        f.button ( { value: 'Salva', "class": 'green', onclick : "address_book.admin._save_category()" } );
        f.set ( 'category_workspace' );
};
//}}}
//{{{address_book.admin.add_category = function ( dest )
address_book.admin.add_category = function ( dest )
{
	$ ( 'title-work', 'Nuova Categoria' ); 
	address_book.admin._category_form();
	$( 'sub_category', '' );
};
//}}}
//{{{address_book.admin.edit_category = function ( id_category )
address_book.admin.edit_category = function ( id_category )
{
	$ ( 'title-work', 'Modifica Categoria' ); 
	address_book.admin._cur_id_parent = id_category ;
	address_book.admin._category_form();
	var v = { action : 'address_book.ajax.get_categ', id_categ : id_category };
	address_book.ajax ( v , function ( data )
        {
		var categ = data [  'categ' ];
		address_book.admin._cur_id_parent = categ [ 'id' ];
		address_book.admin._cur_id_parent_name = categ [ 'text' ];
                var f = liwe.form.get ( 'category_add' );
                categ.iterate ( function ( val, k )
                {
                        f.set_value ( k, val );
                });
		address_book.admin._set_add_info ( f, categ );
	
        });
};
//}}}
//{{{address_book.admin.del_category = function ( id_category, is_sub_category )
address_book.admin.del_category = function ( id_category, is_sub_category )
{
	
	var cback = address_book.admin.category;
	var msg = 'Categoria Eliminata';
	if ( is_sub_category ) 
	{
		cback =  function () { address_book.admin.show_sub_category ( address_book.admin._cur_id_parent );} ;
		msg = 'Sottocategoria Eliminata';
		
	};
	var v = { action : 'address_book.admin.del_categ', id_categ : id_category };
	address_book.admin.ajax ( v , function ( data )
        {
		system.admin.do_alert ( msg, liwe.utils.notifier.mode.NOTICE );
		cback();
	},
	function ( txt, err_descr, err_code, data )
	{
		if ( ! err_descr ) err_descr = txt;
		system.admin.do_alert ( err_descr, liwe.utils.notifier.mode.ALERT );
	});
};
//}}}
//{{{address_book.admin._save_category = function ( is_sub_category )
address_book.admin._save_category = function ( is_sub_category )
{
	var f_name = 'category_add';
	var cback = address_book.admin._category_refresh;
	var msg = 'Categoria Salvata';
	var id_node = address_book.admin._cur_id_parent;
	address_book.admin._cur_id_parent = 0;
	if ( is_sub_category ) 
	{
		f_name = 'sub_category_add';
		cback =  function () { address_book.admin.show_sub_category ( id_node );} ;
		msg = 'Sottocategoria Salvata';
		address_book.admin._cur_id_parent = id_node;
	};

	var f = liwe.form.get ( f_name );
	if ( ! f.check () )
	{
		system.admin.do_alert ( 'Inserire titti i valori', liwe.utils.notifier.mode.WARN );
		return;
	}

	var dct = f.get_values ();

	dct [ 'action' ] = "address_book.admin.set_categ";
	liwe.AJAX.easy ( dct, function ( v ) {
		system.admin.do_alert ( msg, liwe.utils.notifier.mode.NOTICE );
		cback();
	} );
};
//}}}
//{{{address_book.admin._am_sub_category_init = function ( dest_div )
address_book.admin._am_sub_category_init = function ( dest_div )
{
        if ( address_book.admin._am_sub_category ) return;
        address_book.admin._am_sub_category = new AdminManager
                                ( {
                                        'dest_res'    : dest_div,
										'name'	      : 'address_book_sub_catagory',
                                        'list_action' : 'address_book.admin.list_am_sub_categories',
                                        'edit_action' : 'address_book.admin.edit_sub_category',
                                        'del_action' : 'address_book.admin.del_category',
                                        'mod_menu_actions': [ { func: "address_book.admin.add_sub_category()", 'class' : 'add' , label: 'Nuova Sottocategoria' }	],

		                                'filter_form' : address_book.admin._am_sub_category_filter_form,
                                        'filter_form_name' : "sub_category_list_filter"
                                } );

		address_book.admin._am_sub_category.set_row_fields (
                                [
                                        [ "Nome", "text" ]
                                ]
                                );

        address_book.admin._am_sub_category.cbacks [ 'row_manip' ] = address_book.admin._rwman_sub_category;
        address_book.admin._am_sub_category.cbacks [ 'edit_item' ] = function ( id ) { address_book.admin.edit_sub_category ( id ); };
        address_book.admin._am_sub_category.cbacks [ 'del_item' ] = function ( id ) { address_book.admin.del_category ( id, true  ); };
		address_book.admin._am_sub_category.cbacks [ 'noresult' ] = function () { address_book.admin.add_sub_category (); };
        address_book.admin._am_sub_category.cbacks [ 'pre_del' ] = address_book.admin._subcat_pre_del;

		var p = '<div class ="glob-action adm-man-ico am_bin" title="Elimina Selezionati" onclick="AdminManager.del_all_item(\'%(_am_name)s\', true )"></div>';
		address_book.admin._am_sub_category.templates [ 'glob_delete' ] = p
		address_book.admin._am_sub_category.templates [ 'module_single_action' ] = address_book.templates [ 'ADMIN_MODULE_SINGLE_ACTION' ];
};
//}}}
//{{{address_book.admin._subcat_pre_del = function ( am )
address_book.admin._subcat_pre_del = function ( am )
{
	var ids = am.get_selected ();
	if ( ids == -1 ) return true; 
	ids = "|".join ( ids )
	address_book.admin.del_category ( ids, true  )	
	return false;
};
//}}}
//{{{address_book.admin._am_sub_category_filter_form = function ( dest_div )
address_book.admin._am_sub_category_filter_form = function ( dest_div )
{
        var f = new liwe.form.instance ( "sub_category_list_filter" );
        f.hidden ( "id_parent" , address_book.admin._cur_id_parent );
        f.set ( dest_div );
};
//}}}
//{{{address_book.admin._rwman_sub_category = function ( ds, row )
address_book.admin._rwman_sub_category = function ( ds, row )
{
        row [ '_id' ] = row [ 'id' ];
};
//}}}
//{{{address_book.admin.show_sub_category = function ( id_category )
address_book.admin.show_sub_category = function ( id_category )
{
	var v = { action : 'address_book.ajax.get_categ', id_categ : id_category };
	address_book.ajax ( v , function ( data )
        {
		address_book.admin._cur_id_parent_name = data [ 'categ' ] [ 'text' ];
		$( 'title-work' , 'Lista Sotto Categorie di - [ ' + address_book.admin._cur_id_parent_name + ' ]' );
		address_book.admin._cur_id_parent = id_category ;
		address_book.admin._am_sub_category.show_list ( 'category_workspace' );
	});	
};
//}}}
//{{{address_book.admin.add_sub_category = function ()
address_book.admin.add_sub_category = function ()
{
	$( 'title-work' , 'Nuova Sotto Categoria di- [ ' + address_book.admin._cur_id_parent_name + ' ]' );
	address_book.admin._sub_category_form( function ()
	{
		var f = liwe.form.get ( 'sub_category_add' );
	});
}
//}}}
//{{{address_book.admin.edit_sub_category = function ( id_category )
address_book.admin.edit_sub_category = function ( id_category )
{
	$( 'title-work' , 'Modifica Sotto Categoria di - [ ' + address_book.admin._cur_id_parent_name + ' ]' );
	address_book.admin._sub_category_form();
	var v = { action : 'address_book.ajax.get_categ', id_categ : id_category };
	address_book.ajax ( v , function ( data )
        {

                var f = liwe.form.get ( 'sub_category_add' );
                data [ 'categ' ].iterate ( function ( val, k )
                {
                        f.set_value ( k, val );
                });
		f.set_value ( 'id_node', id_category );
		address_book.admin._set_add_info ( f, data [ 'categ' ] );
        });
};
//}}}
//{{{address_book.admin._sub_category_form = function ( cback )
address_book.admin._sub_category_form = function ( cback )
{
	var f = new liwe.form.instance ( 'sub_category_add' );

	liwe.AJAX.easy ( { action: 'address_book.admin.list_categs' }, function ( v ) 
	{
		var categs = v [ 'categs' ];
		var opts = address_book.admin._create_sel_categ ( categs );
		f.hidden ( 'id' , '0' );
       	f.hidden ( 'id_node' , '0');
		f.select ( { label: 'Categoria', name: 'parent_id', value: address_book.admin._cur_id_parent , options: opts  } );
		f.text ( { 'name' : 'text', 'size' : 20, 'maxlength' : 50, 'mandatory' : true, 'label' : 'Sotto Categoria' } );
		f.button ( { value: 'Salva', "class": 'green', onclick : "address_book.admin._save_category( true )" });
		f.set ( 'category_workspace' );
		if ( cback ) cback();
	});

};
//}}}
//{{{address_book.admin._create_sel_categ = function ( v )
address_book.admin._create_sel_categ = function ( v )
{
	var t, l = v.length;
	var res = [ { 'label': '', 'value': '' } ];

	for ( t = 0; t < l; t ++ )
	{
		var c = v [ t ];
		res.push ( { 'label': c.text, 'value': c.id } );
	}

	return res;
};
//}}}
//}}}
//{{{address_book.admin.init = function ( dest_div )
address_book.admin.init = function ( dest_div )
{
	if ( ! address_book.templates )
	{
		address_book.load_templates ( function () { address_book.admin._init ( dest_div); } );
		return;
	}
	address_book.admin._init ( dest_div );
};
//}}}

//{{{ LOOK_UP

address_book.admin.lookup_cback = {  'on_row_click' : null };

address_book.admin.lookup_row_click =  function ( pos )
{
	if ( address_book.admin.lookup_cback [ 'on_row_click' ] )
	{
		var ds = DataSet.get ( 'address_book_lookup');
		var row = ds.rows [ pos ];
		address_book.admin.lookup_cback [ 'on_row_click' ] ( row );
	}
	liwe.lightbox.close();
}


address_book.admin.init_lookup = function ( on_row_click )
{
	var ds = new DataSet ( 'address_book_lookup' );
    ds.templates [ 'table_start' ]  = address_book.templates [ 'ADMIN-LOOKUP-TB-START' ];
    ds.templates [ 'table_header' ] = address_book.templates [ 'ADMIN-LOOKUP-TH' ];
    ds.templates [ 'table_row' ] = address_book.templates ['ADMIN-LOOKUP-TR' ];
    ds.templates [ 'table_footer' ] = address_book.templates ['ADMIN-LOOKUP-TF' ];
    ds.paginator.templates [ 'pag-first' ] = address_book.templates [ 'ADMIN-LOOKUP-PAG-FIRST' ];
    ds.paginator.templates [ 'pag-prev' ] = address_book.templates [ 'ADMIN-LOOKUP-PAG-PREV' ];
    ds.paginator.templates [ 'pag-next' ]  = address_book.templates [ 'ADMIN-LOOKUP-PAG-NEXT' ];
    ds.paginator.templates [ 'pag-last' ]  = address_book.templates [ 'ADMIN-LOOKUP-PAG-LAST' ];
    ds.paginator.templates [ 'pag-pos' ] = address_book.templates [ 'ADMIN-LOOKUP-PAG-POS' ];
    ds.paginator.templates [ 'pag-sep' ] = '';
	ds.cbacks [ 'row_manip' ] = address_book.admin._lookup_row_manip;
	if ( on_row_click ) address_book.admin.lookup_cback [ 'on_row_click' ] = on_row_click;
}

address_book.admin._lookup_row_manip  = function ( ds, row )
{
    row ['_id'] = row [ 'uid'];
};


address_book.admin.lookup = function( dct_vals, on_row_click )
{

	if ( typeof ( on_row_click ) == 'undefined' ) return;
	var ds = DataSet.get ( "address_lookup" );
	if ( typeof ( ds ) == 'undefined' ) address_book.admin.init_lookup ( on_row_click );
	liwe.lightbox.easy ( "lookup-ligthbox", "Seleziona Utenti", 700,500 );
	$('lookup-ligthbox', address_book.templates [ 'ADMIN-LOOKUP-LAYOUT' ] );
	address_book.admin._lookup_search_form ( 'address_book-lookup-search', dct_vals );
	address_book.admin._lookup_start_search  ();
}


address_book.admin._lookup_search_form =  function ( dest_div, data )
{
	var f = new  liwe.form.instance ( 'lookup_search' );
	
	f.events [ 'start' ] = address_book.admin._lookup_start_search;
	f.hidden ( 'company' , data.get ( 'company', 0 ) );
	f.hidden ( 'individual' , data.get ( 'individual', 0 ) );
	f.text ( { 'label' : 'Nome/Ragione sociale', 'name' : 'name', size : 30, 'maxlength' : 30 , value : data.get( 'name' , '' )  } );
	f.text ( { 'label' : 'Cognome', 'name' : 'lastname', size : 30, 'maxlength' : 30, value : data.get ( 'lastname', '' ) } );
	f.cod_fisc ( { 'label' : 'Cod. Fiscale', 'name' : 'tax_code', size : 30, 'maxlength' : 20, 'width' : '100%', value : data.get ( 'cod_fisc', '' ) } );
	f.p_iva ( { 'label' : 'P.IVA', 'name' : 'vat_code', size : 30, 'maxlength' : 20 , value :data.get ( 'vat_code', '' )} );
    f.button ( { "value" : 'Cerca', onclick : 'address_book.admin._lookup_start_search()'  } );
    f.set ( dest_div);
}


address_book.admin._lookup_start_search = function ()
{

    var f = liwe.form.get ( 'lookup_search' );
    var vals = f.get_values();
    vals [ 'action' ] = "address_book.admin.list_profile";
    var ds = DataSet.get ( 'address_book_lookup');
    ds.set_fields ( vals );
    ds.fill ( function () { ds.render ( 'address_book-lookup' ); } );

}

//}}}


